package com.zhijian.medical.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.format.FastDateFormat;
import com.zhijian.medical.annotation.Response;
import com.zhijian.medical.config.CommonConfig;
import com.zhijian.medical.entity.vo.request.PersonDeviceVerifyHistorySaveOrUpdateVo;
import com.zhijian.medical.entity.vo.request.PersonDeviceVerifyHistorySearchVo;
import com.zhijian.medical.entity.vo.response.CodeVo;
import com.zhijian.medical.entity.vo.response.PersonDeviceVerifyHistoryVo;
import com.zhijian.medical.service.PersonDeviceVerifyHistoryService;
import com.zhijian.medical.service.impl.PersonDeviceVerifyHistoryServiceImpl;
import com.zhijian.medical.util.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 设备/人员比对验证实验记录 控制层
 *
 * @author HCG
 * @version 1.0
 * @date 2023-07-17 19:37:29
 */
@Slf4j
@Controller
@Api(tags = "设备-人员比对验证实验记录")
public class PersonDeviceVerifyHistoryController {

    @Autowired
    private PersonDeviceVerifyHistoryService service;

    @Autowired
    private CommonConfig commonConfig;

    /**
     * 获取最新编号们
     *
     * @return
     */
    @Response
    @GetMapping("/person/device/verify/history/code")
    @ApiOperation("获取最新编号们")
    public CodeVo getNewCode() {
        CodeVo codeVo = new CodeVo();
        codeVo.setCode2(service.createNextCode());
        codeVo.setCode(PersonDeviceVerifyHistoryServiceImpl.CODE_PREFIX);
        codeVo.setDw(commonConfig.getDw());
        codeVo.setVersion(commonConfig.getVersion());
        codeVo.setSaveTime(commonConfig.getSaveTime());
        codeVo.setZrr(commonConfig.getZrr());
        codeVo.setPublishDate(DateUtil.format(new Date(), FastDateFormat.getInstance("yyyy年MM月dd日")));
        codeVo.setExecuteDate(codeVo.getPublishDate());
        return codeVo;
    }

    /**
     * 新增
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/person/device/verify/history")
    @ApiOperation("新增")
    public boolean insert(@RequestBody PersonDeviceVerifyHistorySaveOrUpdateVo param) {
        log.info("{} - {}| insert info", this.getClass().getSimpleName(), param);
        return service.save(param);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Response
    @DeleteMapping("/person/device/verify/history/{id}")
    @ApiOperation("删除")
    public boolean delete(@PathVariable("id") Long id) {
        log.info("{} - {}| delete info", this.getClass().getSimpleName(), id);
        service.deleteById(id);
        return true;
    }

    /**
     * 修改
     *
     * @param id
     * @param param
     * @return
     */
    @Response
    @PutMapping("/person/device/verify/history/{id}")
    @ApiOperation("修改")
    public boolean update(@PathVariable("id") Long id, @RequestBody PersonDeviceVerifyHistorySaveOrUpdateVo param) {
        log.info("{} - {}| update info, {}", this.getClass().getSimpleName(), id, param);
        param.setId(id);
        return service.modify(param);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Response
    @GetMapping("/person/device/verify/history/{id}")
    @ApiOperation("根据id查询")
    public PersonDeviceVerifyHistoryVo selectById(@PathVariable("id") Long id) {
        log.info("{} - {}| query info", this.getClass().getSimpleName(), id);
        return service.selectVoById(id);
    }

    /**
     * 分页查询
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/person/device/verify/history/page")
    @ApiOperation("分页查询")
    public Page<PersonDeviceVerifyHistoryVo> selectByCondition(@RequestBody(required = false) PersonDeviceVerifyHistorySearchVo param) {
        if (param == null)
            param = PersonDeviceVerifyHistorySearchVo.builder().build();
        log.info("{} - {}| query info", this.getClass().getSimpleName(), param);
        return service.selectPage(param);
    }

}